<html>
<head><meta charset="utf-8"><title>MIR code generation in rustc_driver callback · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html">MIR code generation in rustc_driver callback</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219992820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/219992820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dominik Stolz <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#219992820">(Dec 15 2020 at 15:16)</a>:</h4>
<p>Hi everyone! I'm experimenting with automatic test generation for Rust code. As I require type information, I want to generate code based on the MIR instead of using a proc-macro. I thought about using rustc a library with the rustc_driver interface. Would it be possible to generate MIR code in the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/trait.Callbacks.html#method.after_analysis"><code>after_analysis</code></a> callback, and then continue the normal compilation?</p>



<a name="219993010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/219993010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#219993010">(Dec 15 2020 at 15:18)</a>:</h4>
<p>Rustc uses a query based architecture which means that MIR generation for one function can be interleaved with typechecking of another. For example when using a <code>const fn</code> inside the length of an array.</p>



<a name="219993053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/219993053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#219993053">(Dec 15 2020 at 15:18)</a>:</h4>
<p>At best you can override the provider for the <code>optimized_mir</code> query.</p>



<a name="220003726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/220003726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dominik Stolz <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#220003726">(Dec 15 2020 at 16:25)</a>:</h4>
<p>Okay thanks! But how would I go about overriding the Provider from the callback? I thought the list of providers is only populated once when the TyCtxt is created.</p>



<a name="220006921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/220006921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#220006921">(Dec 15 2020 at 16:44)</a>:</h4>
<p>You have to override it from <code>config.override_queries</code> which can be accessed in <code>Callbacks::config()</code>. There is no way to change it later on. That would cause inconsistent results anyway as changing the provider won't invalidate any already executed queries.</p>



<a name="220024695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/220024695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dominik Stolz <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#220024695">(Dec 15 2020 at 18:53)</a>:</h4>
<p>Ah thanks! But does this approach only allow to transform existing items or can I also generate new items? Would it be possible to have a custom trait that behaves similar to a auto trait? (i.e. automatically generate implementations for suitable types) Further, it appears to me that overriding the existing provider would mean that <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/transform/fn.run_optimization_passes.html">run_optimization_passes</a> is never called.</p>



<a name="220025840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/MIR%20code%20generation%20in%20rustc_driver%20callback/near/220025840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/MIR.20code.20generation.20in.20rustc_driver.20callback.html#220025840">(Dec 15 2020 at 19:02)</a>:</h4>
<p>You can't add new items without either modifying the source or changing the AST (<code>Queries::parse</code>) from <code>Callbacks::after_parsing</code> or <code>Callbacks::after_expansion</code>. The HIR is immutable.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>